package model.entity;

import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import model.entity.product.Product;
import model.entity.role.Users;

@Entity
@Table(name = "Orders")
public class Orders {

	private int orderId;
	private Users user;
	private String phone;
	private Date orderDateTime;
	private String comment;
	private boolean confirmed;
	private boolean finished;
	private double moneySum;
	private String address;
	private Double latitude;
	private Double longitude;
	private String validationToken;
	private List<OrderDetails> orderDetailses = new ArrayList<OrderDetails>(0);

	public Orders() {
	}

	public Orders(String phone, Date orderDateTime,
			boolean confirmed, boolean finished, double moneySum,
			String address, String validationToken) {
		this.phone = phone;
		this.orderDateTime = orderDateTime;
		this.confirmed = confirmed;
		this.finished = finished;
		this.moneySum = moneySum;
		this.address = address;
		this.validationToken = validationToken;
	}

	public Orders(Users user, String phone, Date orderDateTime,
			String comment, boolean confirmed, boolean finished,
			double moneySum, String address, Double latitude, Double longitude,
			String validationToken, List<OrderDetails> orderDetailses) {
		this.user = user;
		this.phone = phone;
		this.orderDateTime = orderDateTime;
		this.comment = comment;
		this.confirmed = confirmed;
		this.finished = finished;
		this.moneySum = moneySum;
		this.address = address;
		this.latitude = latitude;
		this.longitude = longitude;
		this.validationToken = validationToken;
		this.orderDetailses = orderDetailses;
	}

	@Id
	@GeneratedValue(generator = "Order_ID_Gen", strategy = GenerationType.IDENTITY)
	@SequenceGenerator(name = "Order_ID_Gen", sequenceName = "Order_SEQ_ID")
	@Column(name = "OrderID", unique = true, nullable = false)
	public int getOrderId() {
		return this.orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "UserID")
	public Users getUser() {
		return this.user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	@Column(name = "Phone", nullable = false)
	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "OrderDateTime", nullable = false, length = 23)
	public Date getOrderDateTime() {
		return this.orderDateTime;
	}

	public void setOrderDateTime(Date orderDateTime) {
		this.orderDateTime = orderDateTime;
	}

	@Column(name = "Comment")
	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@Column(name = "Confirmed", nullable = false)
	public boolean isConfirmed() {
		return this.confirmed;
	}

	public void setConfirmed(boolean confirmed) {
		this.confirmed = confirmed;
	}

	@Column(name = "Finished", nullable = false)
	public boolean isFinished() {
		return this.finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	@Column(name = "MoneySum", nullable = false, precision = 53, scale = 0)
	public double getMoneySum() {
		return this.moneySum;
	}

	public void setMoneySum(double moneySum) {
		this.moneySum = moneySum;
	}

	@Column(name = "Address", nullable = false)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "Latitude", precision = 53, scale = 0)
	public Double getLatitude() {
		return this.latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	@Column(name = "Longitude", precision = 53, scale = 0)
	public Double getLongitude() {
		return this.longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	@Column(name = "ValidationToken", nullable = false)
	public String getValidationToken() {
		return this.validationToken;
	}

	public void setValidationToken(String validationToken) {
		this.validationToken = validationToken;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order")
	public List<OrderDetails> getOrderDetailses() {
		return this.orderDetailses;
	}

	public void setOrderDetailses(List<OrderDetails> orderDetailses) {
		this.orderDetailses = orderDetailses;
	}
	
	public static int sumMoney(ArrayList<OrderDetails> cart) {
		int sum = 0;
		for (OrderDetails orderDetails : cart) {
			sum += orderDetails.totalMoney();
		}
		return sum;
	}
	
	public static String sumMoneyS(ArrayList<OrderDetails> cart) {
		int sum = sumMoney(cart);
		return sum + " VNĐ";
	}
	
	public static int sumQuantity(ArrayList<OrderDetails> cart) {
		int sum = 0;
		for (OrderDetails orderDetails : cart) {
			sum += orderDetails.getQuantity();
		}
		return sum;
	}
}
